home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 2 / Mac Magazin and MacEasy Magazine CD - Issue 02.iso / Sharewarebibliothek / Applikationen / Alpha.5.81 folder / Tcl / SystemCode / procs.tcl < prev    next >
Text File  |  1994-06-13  |  11KB  |  456 lines

  1. #==============================================================================
  2. proc normalLeftBracket {} {
  3.     insertText "\{"
  4. }
  5. proc normalRightBracket {} {
  6.     insertText "\}"
  7. }
  8. bind '\[' <zs>  normalLeftBracket
  9. bind '\]' <zs>  normalRightBracket
  10.             
  11. # Select the next or current word. If word already selected, will go to next.
  12. proc hiliteWord {} {
  13.     if {[getPos]!=[selEnd]}    forwardChar
  14.     forwardWord
  15.     set start [getPos]
  16.     backwardWord
  17.     select $start [getPos]
  18. }
  19.  
  20. bind 'h' <z> hiliteWord
  21.  
  22. #================================================================================
  23. # Mode variables
  24. #================================================================================
  25. # For mark stack.
  26. set markName 0
  27. set markStack ""
  28.  
  29. # mapping of windows to current modes.
  30. set winModes("") ""
  31.  
  32. # making vars local to windows
  33. # 'incomingVars' used to hold old var values that have been overwritten in current window
  34.  
  35. #================================================================================
  36. # Handle 'flag' and 'var' menu selections.
  37. #================================================================================
  38. proc editFlag {menu item} {
  39.     global $item incomingVars
  40.  
  41.     set val [expr ([set $item]-1)*-1]
  42.     markMenuItem $menu $item [expr ([set $item])?"on":"off"]
  43.     set $item $val
  44. }
  45.  
  46. proc editVar {menu item} {
  47.     global $item incomingVars
  48.  
  49.     append prmpt "New Value of " $item ": "
  50.     if ![catch {prompt $prmpt [set $item]} res] {
  51.         set $item $res
  52.     }
  53. }
  54.  
  55.  
  56.  
  57.  
  58. #================================================================================
  59.  
  60. # Instantiate a global variable to the path of a file (usually an app). As a
  61. # side-effect, make the instantiation permanent by adding a line to 'definitions.tcl'.
  62. proc addAppPath {name var} {
  63.     global $var
  64.     
  65.     if {[catch {getfile "Find '$name' app:"} path]} {return 1}
  66.     set $var $path
  67.  
  68.     addUserLine "set $var \"[quoteExpr2 $path]\""
  69.     return 0
  70. }
  71.  
  72. proc addUserLine {line} {
  73.     global HOME
  74.  
  75.     if {[file exists "$HOME:userStartup.tcl"]} {
  76.         set fid [open "$HOME:userStartup.tcl" "a"]
  77.     } else {
  78.         set fid [open "$HOME:userStartup.tcl" "w"]
  79.     }
  80.     puts $fid $line
  81.     close $fid
  82. }
  83.  
  84.  
  85. proc getFileSig {f} {
  86.     catch {lindex [ls -l $f] 5} var
  87.     return $var
  88. }
  89.  
  90.  
  91. # Look for given app sig in active processes. If not there, try to 
  92. # launch with 'path' prompting for 'path' if necessary.
  93. # Return the real name of the app. Don't switch.
  94. proc checkRunning {name sig path} {
  95.     global $path
  96.     foreach proc [processes] {
  97.         if {[lindex $proc 1] == $sig} {
  98.             return [lindex $proc 0]
  99.         }
  100.     }
  101.     if {![info exists $path] || ![file exists [set $path]]} {
  102.         if {[addAppPath $name $path]} return
  103.     }
  104.     if {[catch {getFileSig [set $path]}]} {
  105.         if {[addAppPath $name $path]} return
  106.     }
  107.     set sig [getFileSig [set $path]]
  108.     if {[catch {launch -f [set $path]}]} {
  109.         error "Problem with script."
  110.     }
  111.     return [file tail [set $path]]
  112. #    return [checkRunning $name $sig $path]
  113. }
  114.  
  115. #================================================================================
  116. # Excalibur is the only Mac spell-checker that I know of which will handle LaTeX as
  117. # well as ordinary text.
  118.  
  119.  
  120. proc spellcheckWindow {} {
  121.     global excaliburPath resumeRevert
  122.  
  123.     catch {checkRunning Excalibur XCLB excaliburPath} name
  124.  
  125.     if {[winInfo dirty]} {
  126.         if {[askyesno "Save '[lindex [winNames] 0]'?"] == "yes"} {
  127.             save
  128.         }
  129.     }
  130.     if {[catch {sendOpenEvent -n $name [lindex [winNames -f] 0]}] } {
  131.         beep 
  132.     } else {
  133.         switchTo $name
  134.     }
  135.     set resumeRevert 1
  136. }
  137.  
  138. proc spellcheckSelection {} {
  139.     global excaliburPath 
  140.  
  141.     catch {checkRunning Excalibur XCLB excaliburPath} name
  142.  
  143.     if {[getPos] == [selEnd]} {
  144.         beep
  145.         message "No selection"
  146.         return;
  147.     }
  148.     copy
  149.     switchTo $name
  150. }
  151.  
  152. #================================================================================
  153.  
  154.  
  155. proc alphaHelp {} {
  156.     global HOME
  157.     edit -r -m "$HOME:Help:Alpha Commands"
  158. }
  159.  
  160.  
  161. proc tclHelp {} {
  162.     global HOME
  163.     edit -r -m "$HOME:Help:Tcl Commands"
  164. }
  165.  
  166.  
  167. set patternLibrary {
  168.     { "Pascal to C Comments" {        \{([^\}]*)\}} {/* \1 */} }
  169.     { "C++ to C Comments" {//(.*)} {/* \1 */}}
  170. }
  171.  
  172.  
  173. proc dividingLine {} {
  174.     insertText "================================================================================\r"
  175. }
  176. bind 'l' <C> dividingLine
  177.  
  178. proc texDividingLine {} {
  179.     insertText "%================================================================================\r"
  180. }
  181. bind 'l' <C> texDividingLine TeX
  182.  
  183. proc cDividingLine {} {
  184.     insertText "//================================================================================\r"
  185. }
  186. bind 'l' <C> cDividingLine C
  187. bind 'l' <C> cDividingLine C++
  188.  
  189. proc tclDividingLine {} {
  190.     insertText "#================================================================================\r"
  191. }
  192. bind 'l' <C> tclDividingLine Tcl
  193.  
  194.  
  195. #================================================================================
  196.  
  197. if {[catch {info args oldCd}]} {
  198.     rename cd oldCd
  199. }
  200.  
  201. proc cd args {
  202.     global HOME
  203.     if {[llength $args]} {
  204.         oldCd [string trim [eval list $args] "        \{\}"]
  205.     } else {
  206.         oldCd $HOME
  207.     }
  208. }
  209.  
  210. #================================================================================
  211.  
  212. proc getVarValue {} {
  213.     set val [listpick -p {Which var?} [lsort [info globals]]]
  214.     if {![string length $val]} return
  215.     global $val
  216.     alertnote [join [list "'$val' = " [set $val]] ""]
  217. }
  218.  
  219. #================================================================================
  220.     
  221. proc selectParagraph {} {
  222.     set pos [getPos]
  223.     set start [paraStart $pos] 
  224.     set finish [paraFinish $pos]
  225.     goto $start
  226.     select $start $finish
  227. }
  228.  
  229. # wrapText ==  getText ; breakIntoLines ; replaceText
  230. # Remove text from window, transform (join, del-ws), insert back into window.
  231. proc fillTextByPar {from to} {
  232.     set text [getText $from $to]
  233.     regsub -all "\r(\[ \t\]*\r)+" $text "\r\r\r" text
  234.     regsub -all "(\[^\r\])\r" $text "\\1 " text
  235.     regsub -all "\[ \t\]+" $text " " text
  236.     return [breakIntoLines $text]
  237. }
  238.  
  239. proc fillRegionByPar {{start -1} {finish -1}} {
  240. #    # if {[getPos] == [selEnd]} { return}
  241.     if {($start < 0) || ($finish < 0)} {
  242.         set start [lineStart [getPos]]
  243.         set finish [selEnd] }
  244.     if {$start >= $finish} return
  245.     goto $start
  246.     set text [fillTextByPar $start $finish]
  247.     replaceText $start $finish $text "\r"
  248. }
  249.     
  250. #
  251. # join Lines in region -- if no optional args, use selection
  252. #
  253. proc joinRegion {{from -1} {to -1}} {
  254.     if {($from < 0) || ($to < 0)} {    set from [getPos] ; set to [selEnd] }
  255.     if {$from >= $to} return
  256.     set text [getText $from $to]
  257.     regsub -all "\r(\[ \t\]*\r)+" $text "\r\r\r" text
  258.     regsub -all "(\[^\r\])\r" $text "\\1 " text
  259.     replaceText $from $to $text "\r"
  260. }
  261. # WARNING:    regsub ^$ refers to string endpts (not lines)
  262. # FUTURE:    filterLines like perl:
  263. #    replaceText[apply_to_all(cmd,split [getText [getPos] [selEnd]] "\r")]
  264. # OR:    replaceInRegion: dup_\r, $=>\r ??
  265. #
  266.  
  267.  
  268. #
  269. # Remove text from window, transform (delete dup ws), insert back into window.
  270. #
  271. # inputs: message, alertnote, askyesno, listpick, prompt KILLS SELECTION.
  272. # search: bnds = search -forward -regExpr -ignoreCase -matchWords -noabort 
  273. #        -l limit pat pos
  274. proc regsubInRegion {from to srch repl} {
  275.     if {![string length $srch]} return
  276.     if {$from >= $to} return
  277.     set text [getText $from $to]
  278.     regsub -all "$srch" $text "$repl" text
  279.     replaceText $from $to $text
  280. }
  281. #    while {($pos < $to) &&
  282. #          ![catch {search -f 1 -r 1 -i 1 -m 0 "$srch" $pos} mtch]} {
  283. #        set mbeg [lindex $mtch 0]
  284. #        set pos [lindex $mtch 1]
  285. #        replaceText $mbeg $pos $repl }
  286.  
  287. #proc backSlashSub {arg} { eval [concat return "\"$arg\""] }
  288.  
  289. proc backSlashSub {arg} {
  290.     regsub -all {\\} $arg {\\\\} arg
  291.     regsub -all {\[} $arg {\\[} arg
  292.     regsub -all {\]} $arg {\\]} arg
  293.     eval [concat return "\"$arg\""]
  294. }
  295.  
  296. proc replaceInRegion {} {
  297.     if [catch {prompt "Search RegExpr:" ""} srch] return
  298.     if [catch {prompt "Replace String:" ""} repl] return
  299.     if {![string length $srch]} return
  300.     regsubInRegion [getPos] [selEnd] \
  301.         [backSlashSub "$srch"] [backSlashSub "$repl"]
  302. }
  303.  
  304. #
  305. # Apply command to each line (or paragraph) in selection ;
  306. #    if no cmd arg then prompts for it
  307. #
  308. proc filterLines {{cmd 0} {parunit 0}} {
  309.     if {$cmd == 0} {
  310.       if {[catch { prompt "Line-filter command: " "" } cmd]} { return } }
  311.     if {![string length $cmd]} return
  312.     set unitStart lineStart
  313.     set unitEnd nextLineStart
  314.     if {$parunit} {
  315.         set unitStart paraStart
  316.         set unitEnd paraFinish }
  317.     set pos [$unitStart [getPos]]
  318.     set finish [selEnd]
  319.     if {$pos >= $finish} return
  320.     goto $pos
  321.     createTMark "filterLend" $finish
  322.     set next [$unitEnd $pos]
  323.     while {(($next > $pos) && ($pos < $finish))} {
  324.         goto [expr $next-1]
  325.         createTMark "filterLnext" $next
  326.         setMark
  327.         goto $pos
  328.         markHilite
  329.         if {[catch [list uplevel #0 "$cmd"] retval]} {
  330.             select $pos $finish
  331.             alertnote $retval
  332.             return
  333.         }
  334.         if {$next==$finish} break
  335.         set ind [lsearch -regexp [lindex [getTMarks] 0] "filterLend.*"]
  336.         set finish [lindex [lindex [lindex [getTMarks] 0] $ind] 2]
  337.         gotoTMark "filterLnext"
  338.         set pos [$unitStart [getPos]]
  339.         set next [$unitEnd $pos]
  340.     }
  341.     removeTMark "filterLend"
  342.     removeTMark "filterLnext"
  343. }
  344.  
  345.  
  346. proc filterParagraphs {{cmd 0}} { filterLines $cmd 1 }
  347.  
  348. # WARNING: deselecting sets the mark to selEnd
  349. proc sortParagraphs {{from -1} {to -1}} {
  350.     if {($from < 0) || ($to < 0)} {    set from [getPos] ; set to [selEnd] }
  351.     if {$from >= $to} return
  352.     joinRegion {$from $to}
  353.     select [getPos] [nextLineStart [getMark]]
  354.     sortLines
  355.     select [getPos] [getPos]
  356.     regsubInRegion [getPos] [getMark] "\r" "\r\r" 
  357.     wrapRegion
  358. }
  359.  
  360. #
  361. # Sample
  362. #
  363. proc filterRegion {{from -1} {to -1} {cmd 0} {newwin 0}} {
  364.     if {$cmd == 0} {
  365.       if {[catch { prompt "Eval command: " "" } cmd]} { return }
  366.     }
  367.     if {![string length $cmd]} return
  368.     if {($from < 0) || ($to < 0)} {    set from [getPos] ; set to [selEnd] }
  369.     if {$from >= $to} return
  370.     set pos [getPos]
  371.     set text [getText $from $to]
  372.     set text [$cmd $text]
  373.     replaceText $from $to $text "\r"
  374.     goto $pos
  375. }
  376.  
  377.  
  378. #
  379. set lastEvaled ""
  380. proc evaluate {} {
  381.     global lastEvaled
  382.     if {[string length $lastEvaled]} {
  383.         set p "M-x ($lastEvaled): "
  384.     } else {
  385.         set p "M-x: "
  386.     }
  387.     if {[catch {statusPrompt $p} text]} {return}
  388.     if {![string length $text]} {set text $lastEvaled}
  389.     $text
  390.     set lastEvaled $text
  391. }
  392.  
  393.  
  394. # First, define macros to bypass the electric braces.
  395. proc ordLeftBrace {} {
  396.     insertText "        \{"
  397. }
  398. bind {'['} <cs> ordLeftBrace
  399.  
  400. proc ordRightBrace {} {
  401.     insertText "\}"
  402.     blink [matchIt "\}" [expr [getPos]-1]]
  403. }
  404. bind {']'} <cs> ordRightBrace
  405.     
  406. proc quoteWord {} {
  407.     backwardWord
  408.     insertText "'"
  409.     forwardWord
  410.     insertText "'"
  411. }
  412. bind ''' <z> quoteWord
  413.  
  414. #================================================================================
  415.  
  416. proc tomac {fname} {
  417.     set fd [open $fname "r"]
  418.     set text [read $fd]
  419.     close $fd
  420.     set fd [open $fname "w"]
  421.     regsub "\n" $text "\r" text
  422.     puts -nonewline $fd $text
  423.     close $fd
  424. }
  425.  
  426. proc tounix {fname} {
  427.     set fd [open $fname "r"]
  428.     set text [read $fd]
  429.     close $fd
  430.     set fd [open $fname "w"]
  431.     regsub "\r" $text "\n" text
  432.     puts -nonewline $fd $text
  433.     close $fd
  434. }
  435.  
  436. #================================================================================
  437. # Sets marks for file.
  438.  
  439. set mpos("") ""
  440.  
  441. proc markFile {} {
  442.     global mode
  443.  
  444.     case $mode in {
  445.         "C" {return [cMarkFile]}
  446.         "TeX" {return [texMarkFile]}
  447.          "C++" {return [c++MarkFile]}
  448.         "Csh" {return [cshMarkFile]}
  449.         "Tcl" {return [tclMarkFile]}
  450.         "BRWZ" {return [browseMarkFile]}
  451.     }
  452. }
  453.  
  454.  
  455.